from django import shortcuts
from django.contrib import auth
from django.contrib.auth import models as auth_models
from django.contrib.auth import forms as auth_forms
from django.views.generic import list_detail
from django.views.generic import create_update
from django.contrib.auth import decorators
from django.contrib.auth import views as auth_views
import datetime

from coolblog import forms
from coolblog import models
from coolblog import utils

@decorators.login_required()
def create_article(request):
    if request.method == 'POST':
        request.POST = request.POST.copy()
        request.POST['author'] = request.user.id
    
    return create_update.create_object(
        request,
        form_class=forms.ArticleForm,
        post_save_redirect='/admin/article/list/',
        template_name='admin/article_form.html'
    )

@decorators.login_required()
def article_list(request):
    if 'page' in request.GET:
        page = request.GET['page']
    else:
        page = 1

    extra_context = {'article_count': models.Article.objects.count}
    return list_detail.object_list(
        request,
        queryset=models.Article.objects.all(),
        template_name='admin/article_list.html',
        template_object_name='article',
        extra_context=extra_context,
        page=page,
        paginate_by=10
    )

@decorators.login_required()
def user_list(request):
    return list_detail.object_list(
        request,
        queryset=auth_models.User.objects.all(),
        template_name='admin/user_list.html',
        template_object_name='user'
    )

@decorators.login_required()
def delete_article(request, article_id):
    request.method = 'POST'
    return create_update.delete_object(
        request,
        model=models.Article,
        object_id=article_id,
        post_delete_redirect='/admin/article/list/',
    )

@decorators.login_required()
def update_article(request, article_id):
    if request.method == 'POST':
        request.POST = request.POST.copy()
        request.POST['author'] = request.user.id

    return create_update.update_object(
        request,
        form_class=forms.ArticleForm,
        object_id=article_id,
        post_save_redirect='/admin/article/list/',
        template_name='admin/article_form.html',
        template_object_name='article'
    )

@decorators.login_required()
def category_list(request):
    if 'page' in request.GET:
        page = request.GET['page']
    else:
        page = 1

    return list_detail.object_list(
        request,
        queryset=models.Category.objects.all(),
        template_name='admin/category_list.html',
        template_object_name='category',
        page=page,
        paginate_by=10
    )

@decorators.login_required()
def create_category(request):
    return create_update.create_object(
        request,
        form_class=forms.CategoryForm,
        post_save_redirect='/admin/category/list/',
        template_name='admin/category_form.html'
    )

@decorators.login_required()
def update_category(request, cat_id):
    return create_update.update_object(
        request,
        form_class=forms.CategoryForm,
        object_id=cat_id,
        post_save_redirect='/admin/category/list/',
        template_name='admin/category_form.html',
        template_object_name='category'
    )

@decorators.login_required()
def delete_category(request, cat_id):
    request.method = 'POST'
    response = create_update.delete_object(
        request,
        model=models.Category,
        object_id=cat_id,
        post_delete_redirect='/admin/category/list/',
    )

    return response

@decorators.login_required()
def blog_settings(request):
    return create_update.update_object(
        request,
        form_class=forms.BlogForm,
        object_id=1,
        post_save_redirect='/admin/settings/',
        template_name='admin/blog_form.html',
        template_object_name='blog'
    )
